// deviceList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    changeIntoIdentification:'',//转入身份证号
    deviceList: [],
    hiddenmodalput: true,//可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框
    thatIndex:0,
    device:{},
  },
  //点击按钮痰喘指定的hiddenmodalput弹出框 
  //取消按钮  
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认  
  confirm: function (e) {
    var that=this
    that.setData({
      hiddenmodalput: true
    });
  },
  addDdevice:function(e){
    wx.navigateTo({
      url: '/page-wode/deviceInformation/deviceInformation'
    })
  },
  buttonShow:function(e){
    var that=this;
    that.setData({
      thatIndex: parseInt(e.currentTarget.id),
      device: that.data.deviceList[that.data.thatIndex]
    })
    wx.showActionSheet({
      itemList: ['过户/转移', '金融', '保险'],
      success: function (res) {
        var tpx=res.tapIndex;
        if (tpx==0){
          //显示输入身份证的框
          that.setData({
            hiddenmodalput: !that.data.hiddenmodalput
          })
        }else if (tpx == 1){
          //金融
        }else if (tpx == 2){
          //保险
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  changeIntoIdentification:function(e){
    var that=this;
    that.setData({
      changeIntoIdentification: e.detail.value
    });
    var changeIntoIdentification = e.detail.value;
    var identification = wx.getStorageSync("identification");
    if (identification != changeIntoIdentification){
      wx.request({
        url: 'https://www.gcjxglzx.com/userInfo/' + changeIntoIdentification +'/getUserInfoByCondition/',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.status == '0'){
            var registerName=res.data.data[0].registerName;
            var len = registerName.length - 1;
            var xing = '';
            for (var i = 0; i < len; i++) {
              xing+= '*';
            }
            that.setData({
              hiddenmodalput: true
            });
            var name=registerName.substring(0, 1) + xing;
            wx.showModal({
              title: '是否将设备转给',
              content: name,
              success: function (res) {
                if (res.confirm) {
                  var factoryNum = that.data.device.factoryNum;
                  var plateNum = that.data.device.plateNum
                  if (factoryNum != null && factoryNum != null){
                    wx.request({
                      url: 'https://www.gcjxglzx.com/transferLicense/save',
                      method: 'POST',
                      data: {
                        identification: identification,
                        changeIntoIdentification: changeIntoIdentification,
                        state: '0',
                        factoryNum: factoryNum,
                        plateNum: plateNum
                      },
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      success: function (res) {

                      }
                    })
                  }
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }else{
            wx.showToast({
              title: '提醒转入人注册或重新添加转入人',
              duration: 4000,
              mask: true
            });
            that.setData({
              hiddenmodalput: false
            });
          }
        }
      })
    } else if (identification == changeIntoIdentification){
      wx.showToast({
        title: '转入人不能为本人',
        duration: 4000,
        mask:true
      })
    }
      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      identification: wx.getStorageSync("identification")
    })
    var identification = that.data.identification;
    wx.request({
      url: 'https://www.gcjxglzx.com/deviceInformation/' + identification + '/getDeviceInformationByCondition',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        for (var i = 0; i < res.data.data.length; i++) {
          if (res.data.data[i].machineryType == '1') {
            res.data.data[i].machineryType = "挖掘机"
          }
          if (res.data.data.machineryType == '2') {
            res.data.data[i].machineryType = "装载机"
          }
          if (res.data.data[i].machineryType == '3') {
            res.data.data[i].machineryType = "矿用自卸车"
          }
          if (res.data.data[i].machineryType == '4') {
            res.data.data[i].machineryType = "推土机"
          }
          if (res.data.data[i].machineryType == '5') {
            res.data.data[i].machineryType = "压路机"
          }
          if (res.data.data[i].machineryType == '6') {
            res.data.data[i].machineryType = "平地机"
          }
          if (res.data.data[i].machineryType == '7') {
            res.data.data[i].machineryType = "混凝土机械"
          }
          if (res.data.data[i].machineryType == '8') {
            res.data.data[i].machineryType = "桩工机械"
          }
          if (res.data.data[i].machineryType == '8') {
            res.data.data[i].machineryType == "凿岩机械"
          }
        }
        if (res.data.data != null && res.data.data.length > 0) {
          that.setData({
            deviceList: res.data.data
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})